package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.CreatePusherBeen;
import cn.xfdzx.android.apps.shop.bean.LogininfoBean;
import cn.xfdzx.android.apps.shop.bean.VideoInfoBeen;
import cn.xfdzx.android.apps.shop.bean.VideoRoomGoodListBeen;
import cn.xfdzx.android.apps.shop.contract.CreatePusherContract;
import cn.xfdzx.android.apps.shop.contract.VideoInfoContract;
import cn.xfdzx.android.apps.shop.model.CreatePusherModel;
import cn.xfdzx.android.apps.shop.model.VideoInfoModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class VideoInfoPresenter extends BasePresenter<VideoInfoContract.View> implements VideoInfoContract.Presenter {

    private VideoInfoContract.Model model;

    public VideoInfoPresenter() {
        model = new VideoInfoModel();
    }


    @Override
    public void videoInfo(String live_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.videoInfo(live_id)
                .compose(RxScheduler.<VideoInfoBeen>Flo_io_main())
                .as(mView.<VideoInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<VideoInfoBeen>() {
                    @Override
                    public void accept(VideoInfoBeen bean) throws Exception {
                        mView.onVideoInfoSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    /**
     * IM登录
     * */
    @Override
    public void getLiveImLogin(String id,String type) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getLiveImLogin(id,type)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.getLiveImLoginSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


    @Override
    public void videoRoomGoodList(String live_id) {
        if (!isViewAttached()) {
            return;
        }
        //  mView.showLoading();
        model.videoRoomGoodList(live_id)
                .compose(RxScheduler.<VideoRoomGoodListBeen>Flo_io_main())
                .as(mView.<VideoRoomGoodListBeen>bindAutoDispose())
                .subscribe(new Consumer<VideoRoomGoodListBeen>() {
                    @Override
                    public void accept(VideoRoomGoodListBeen bean) throws Exception {
                        mView.onVideoRoomGoodListSuccess(bean);
                        //   mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //   mView.hideLoading();
                    }
                });
    }


    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum,String relation_type, String relation_id ) {
        if (!isViewAttached()) {
            return;
        }

        model.addCar(is_new,uniqueId,productId,cartNum,relation_type,relation_id) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);

                    }
                });
    }

    @Override
    public void getLiveRoomFocus(String live_id) {
        if (!isViewAttached()) {
            return;
        }
        //  mView.showLoading();
        model.getLiveRoomFocus(live_id)
                .compose(RxScheduler.<VideoRoomGoodListBeen>Flo_io_main())
                .as(mView.<VideoRoomGoodListBeen>bindAutoDispose())
                .subscribe(new Consumer<VideoRoomGoodListBeen>() {
                    @Override
                    public void accept(VideoRoomGoodListBeen bean) throws Exception {
                        mView.onLiveRoomFocusSuccess(bean);
                        //   mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //   mView.hideLoading();
                    }
                });
    }

    @Override
    public void getLiveRoomZan(String live_id) {
        if (!isViewAttached()) {
            return;
        }
        //  mView.showLoading();
        model.getLiveRoomZan(live_id)
                .compose(RxScheduler.<VideoRoomGoodListBeen>Flo_io_main())
                .as(mView.<VideoRoomGoodListBeen>bindAutoDispose())
                .subscribe(new Consumer<VideoRoomGoodListBeen>() {
                    @Override
                    public void accept(VideoRoomGoodListBeen bean) throws Exception {
                        mView.onLiveRoomZanSuccess(bean);
                        //   mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //   mView.hideLoading();
                    }
                });
    }
}
